package com.fulihui.information.core.repository.impl;

import com.fulihui.information.biz.conv.CommentsConv;
import com.fulihui.information.core.repository.CommentsRepository;
import com.fulihui.information.dal.dataobj.InfoComments;
import com.fulihui.information.dal.mapper.InfoCommentsMapper;
import com.fulihui.information.dto.CommentsDTO;
import com.fulihui.information.request.CommentsRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * The type Comments repository.
 */
@Repository
public class CommentsRepositoryImpl implements CommentsRepository {

    /**
     * The Info comments mapper.
     */
    @Autowired
    InfoCommentsMapper infoCommentsMapper;

    /**
     * 保存评论
     * 
     * @see com.fulihui.information.core.repository.CommentsRepository#saveComment(com.fulihui.information.request.CommentsRequest)
     */
    @Override
    public boolean saveComment(CommentsRequest request) {
        InfoComments record = new InfoComments();
        record.setContent(request.getContent());
        record.setDiscussId(request.getDiscussId());
        record.setUserId(request.getUserId());
        record.setToUserId(request.getToUserId());
        record.setToUserFace(request.getToUserFace());
        record.setToUserNickname(request.getToUserNickname());
        record.setNewsId(request.getNewsId());
        record.setUserFace(request.getUserFace());
        record.setUserNickname(request.getUserNickname());
        record.setNewsDigest(request.getNewsDigest());
        record.setFabulousCount(0L);
        if (request.getDiscussId() == null)
            record.setDiscussTime(new Date());

        record.setGmtCreate(new Date());
        record.setIsDelete("0");
        record.setIsLeaf(request.getDiscussId() == null ? "1" : "0");

        return infoCommentsMapper.insertSelective(record);
    }

    @Override
    public List<CommentsDTO> queryByPage(CommentsRequest request) {
        int start = request.start4Mysql();
        int rows = request.getRows();
        List<InfoComments> comments = infoCommentsMapper.queryByPage(request.getNewsId(),
            request.getUserId(), request.getToUserId(), start, rows);
        return CommentsConv.convert2List(comments);
    }

    @Override
    public int queryByCount(CommentsRequest request) {
        return infoCommentsMapper.queryByCount(request.getNewsId(), request.getUserId(),
            request.getToUserId());
    }

    @Override
    public List<CommentsDTO> selectByDiscussId(Long discussId) {
        List<InfoComments> comments = infoCommentsMapper.selectByDiscussId(discussId);
        return CommentsConv.convert2List(comments);
    }

    /**
     * 根据评论id 查询
     *
     * @param id
     * @return
     */
    @Override
    public CommentsDTO queryById(Long id) {
        InfoComments infoComments = infoCommentsMapper.selectByPrimaryKey(id);
        return CommentsConv.convert2DTO(infoComments);
    }

    /**
     * 根据id 删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        return infoCommentsMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 修改评论点赞次数 +1
     *
     * @param id
     * @return
     */
    @Override
    public boolean modifyAddFabulousCount(Long id) {
        return infoCommentsMapper.updateFabulousCount(id);
    }

}
